home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / kgdb.sun3 / infcmd.c.old < prev    next >
Text File  |  1989-08-06  |  30KB  |  1,164 lines

  1. /* Memory-access and commands for inferior process, for GDB.
  2.    Copyright (C) 1986, 1987, 1988, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. GDB is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GDB is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GDB; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "defs.h"
  21. #include "param.h"
  22. #include "symtab.h"
  23. #include "frame.h"
  24. #include "inferior.h"
  25. #include "environ.h"
  26. #include "value.h"
  27.  
  28. #include <stdio.h>
  29. #include <signal.h>
  30. #include <sys/param.h>
  31.  
  32. extern char *sys_siglist[];
  33.  
  34. #define ERROR_NO_INFERIOR \
  35.    if (inferior_pid == 0) error ("The program is not being run.");
  36.  
  37. /* String containing arguments to give to the program,
  38.    with a space added at the front.  Just a space means no args.  */
  39.  
  40. static char *inferior_args;
  41.  
  42. /* File name for default use for standard in/out in the inferior.  */
  43.  
  44. char *inferior_io_terminal;
  45.  
  46. /* Pid of our debugged inferior, or 0 if no inferior now.  */
  47.  
  48. int inferior_pid;
  49.  
  50. /* Last signal that the inferior received (why it stopped).  */
  51.  
  52. int stop_signal;
  53.  
  54. /* Address at which inferior stopped.  */
  55.  
  56. CORE_ADDR stop_pc;
  57.  
  58. /* Stack frame when program stopped.  */
  59.  
  60. FRAME_ADDR stop_frame_address;
  61.  
  62. /* Number of breakpoint it stopped at, or 0 if none.  */
  63.  
  64. int stop_breakpoint;
  65.  
  66. /* Nonzero if stopped due to a step command.  */
  67.  
  68. int stop_step;
  69.  
  70. /* Nonzero if stopped due to completion of a stack dummy routine.  */
  71.  
  72. int stop_stack_dummy;
  73.  
  74. /* Nonzero if stopped due to a random (unexpected) signal in inferior
  75.    process.  */
  76.  
  77. int stopped_by_random_signal;
  78.  
  79. /* Range to single step within.
  80.    If this is nonzero, respond to a single-step signal
  81.    by continuing to step if the pc is in this range.  */
  82.  
  83. CORE_ADDR step_range_start; /* Inclusive */
  84. CORE_ADDR step_range_end; /* Exclusive */
  85.  
  86. /* Stack frame address as of when stepping command was issued.
  87.    This is how we know when we step into a subroutine call,
  88.    and how to set the frame for the breakpoint used to step out.  */
  89.  
  90. FRAME_ADDR step_frame_address;
  91.  
  92. /* 1 means step over all subroutine calls.
  93.    -1 means step over calls to undebuggable functions.  */
  94.  
  95. int step_over_calls;
  96.  
  97. /* If stepping, nonzero means step count is > 1
  98.    so don't print frame next time inferior stops
  99.    if it stops due to stepping.  */
  100.  
  101. int step_multi;
  102.  
  103. #ifndef KGDB
  104. /* Environment to use for running inferior,
  105.    in format described in environ.h.  */
  106.  
  107. struct environ *inferior_environ;
  108. #endif
  109.  
  110. CORE_ADDR read_pc ();
  111. struct command_line *get_breakpoint_commands ();
  112.  
  113.  
  114. int
  115. have_inferior_p ()
  116. {
  117.   return inferior_pid != 0;
  118. }
  119. #ifndef KGDB
  120. static void 
  121. set_args_command (args)
  122.      char *args;
  123. {
  124.   free (inferior_args);
  125.   if (!args) args = "";
  126.   inferior_args = concat (" ", args, "");
  127. }
  128.  
  129. void
  130. tty_command (file)
  131.      char *file;
  132. {
  133.   if (file == 0)
  134.     error_no_arg ("terminal name for running target process");
  135.  
  136.   inferior_io_terminal = savestring (file, strlen (file));
  137. }
  138.  
  139. static void
  140. run_command (args, from_tty)
  141.      char *args;
  142.      int from_tty;
  143. {
  144.   extern char **environ;
  145.   register int i;
  146.   char *exec_file;
  147.   char *allargs;
  148.  
  149.   extern int sys_nerr;
  150.   extern char *sys_errlist[];
  151.   extern int errno;
  152.  
  153.   dont_repeat ();
  154.  
  155.   if (inferior_pid)
  156.     {
  157.       if (
  158.       !query ("The program being debugged has been started already.\n\
  159. Start it from the beginning? "))
  160.     error ("Program not restarted.");
  161.       kill_inferior ();
  162.     }
  163.  
  164.   exec_file = (char *) get_exec_file (1);
  165.  
  166.   if (remote_debugging)
  167.     {
  168.       if (from_tty)
  169.     {
  170.       printf ("Starting program: %s\n", exec_file);
  171.       fflush (stdout);
  172.     }
  173.     }
  174.   else
  175.     {
  176.       if (args)
  177.     set_args_command (args);
  178.  
  179.       if (from_tty)
  180.     {
  181.       printf ("Starting program: %s%s\n",
  182.           exec_file, inferior_args);
  183.       fflush (stdout);
  184.     }
  185.  
  186. #ifdef sprite
  187.       allargs = concat ("exec debug ", exec_file, inferior_args);
  188. #else
  189.       allargs = concat ("exec ", exec_file, inferior_args);
  190. #endif
  191.       inferior_pid = create_inferior (allargs, environ_vector (inferior_environ));
  192.     }
  193.  
  194.   clear_proceed_status ();
  195.  
  196.   start_inferior ();
  197. }
  198. #endif
  199.  
  200. void
  201. cont_command (proc_count_exp, from_tty)
  202.      char *proc_count_exp;
  203.      int from_tty;
  204. {
  205.   ERROR_NO_INFERIOR;
  206.  
  207.   clear_proceed_status ();
  208.  
  209.   /* If have argument, set proceed count of breakpoint we stopped at.  */
  210.  
  211.   if (stop_breakpoint > 0 && proc_count_exp)
  212.     {
  213.       set_ignore_count (stop_breakpoint,
  214.             parse_and_eval_address (proc_count_exp) - 1,
  215.             from_tty);
  216.       if (from_tty)
  217.     printf ("  ");
  218.     }
  219.  
  220.   if (from_tty)
  221.     printf ("Continuing.\n");
  222.  
  223.   proceed (-1, -1, 0);
  224. }
  225.  
  226. /* Step until outside of current statement.  */
  227. static void step_1 ();
  228.  
  229. static void
  230. step_command (count_string)
  231. {
  232.   step_1 (0, 0, count_string);
  233. }
  234.  
  235. /* Likewise, but skip over subroutine calls as if single instructions.  */
  236.  
  237. static void
  238. next_command (count_string)
  239. {
  240.   step_1 (1, 0, count_string);
  241. }
  242.  
  243. /* Likewise, but step only one instruction.  */
  244.  
  245. static void
  246. stepi_command (count_string)
  247. {
  248.   step_1 (0, 1, count_string);
  249. }
  250.  
  251. static void
  252. nexti_command (count_string)
  253. {
  254.   step_1 (1, 1, count_string);
  255. }
  256.  
  257. static void
  258. step_1 (skip_subroutines, single_inst, count_string)
  259.      int skip_subroutines;
  260.      int single_inst;
  261.      char *count_string;
  262. {
  263.   register int count = 1;
  264.  
  265.   ERROR_NO_INFERIOR;
  266.   count = count_string ? parse_and_eval_address (count_string) : 1;
  267.  
  268.   for (; count > 0; count--)
  269.     {
  270.       clear_proceed_status ();
  271.  
  272.       step_frame_address = FRAME_FP (get_current_frame ());
  273.  
  274.       if (! single_inst)
  275.     {
  276.       find_pc_line_pc_range (stop_pc, &step_range_start, &step_range_end);
  277.       if (step_range_end == 0)
  278.         {
  279.           int misc;
  280.  
  281.           misc = find_pc_misc_function (stop_pc);
  282.           terminal_ours ();
  283.           printf ("Current function has no line number information.\n");
  284.           fflush (stdout);
  285.  
  286.           /* No info or after _etext ("Can't happen") */
  287.           if (misc == -1 || misc == misc_function_count - 1)
  288.         error ("No data available on pc function.");
  289.  
  290.           printf ("Single stepping until function exit.\n");
  291.           fflush (stdout);
  292.  
  293.           step_range_start = misc_function_vector[misc].address;
  294.           step_range_end = misc_function_vector[misc + 1].address;
  295.         }
  296.     }
  297.       else
  298.     {
  299.       /* Say we are stepping, but stop after one insn whatever it does.
  300.          Don't step through subroutine calls even to undebuggable
  301.          functions.  */
  302.       step_range_start = step_range_end = 1;
  303.       if (!skip_subroutines)
  304.         step_over_calls = 0;
  305.     }
  306.  
  307.       if (skip_subroutines)
  308.     step_over_calls = 1;
  309.  
  310.       step_multi = (count > 1);
  311.       proceed (-1, -1, 1);
  312.       if (! stop_step)
  313.     break;
  314.     }
  315. }
  316.  
  317. /* Continue program at specified address.  */
  318.  
  319. static void
  320. jump_command (arg, from_tty)
  321.      char *arg;
  322.      int from_tty;
  323. {
  324.   register CORE_ADDR addr;
  325.   struct symtabs_and_lines sals;
  326.   struct symtab_and_line sal;
  327.  
  328.   ERROR_NO_INFERIOR;
  329.  
  330.   if (!arg)
  331.     error_no_arg ("starting address");
  332.  
  333.   sals = decode_line_spec_1 (arg, 1);
  334.   if (sals.nelts != 1)
  335.     {
  336.       error ("Unreasonable jump request");
  337.     }
  338.  
  339.   sal = sals.sals[0];
  340.   free (sals.sals);
  341.  
  342.   if (sal.symtab == 0 && sal.pc == 0)
  343.     error ("No source file has been specified.");
  344.  
  345.   if (sal.pc == 0)
  346.     sal.pc = find_line_pc (sal.symtab, sal.line);
  347.  
  348.   {
  349.     struct symbol *fn = get_frame_function (get_current_frame ());
  350.     struct symbol *sfn = find_pc_function (sal.pc);
  351.     if (fn != 0 && sfn != fn
  352.     && ! query ("Line %d is not in `%s'.  Jump anyway? ",
  353.             sal.line, SYMBOL_NAME (fn)))
  354.       error ("Not confirmed.");
  355.   }
  356.  
  357.   if (sal.pc == 0)
  358.     error ("No line %d in file \"%s\".", sal.line, sal.symtab->filename);
  359.  
  360.   addr = sal.pc;
  361.  
  362.   clear_proceed_status ();
  363.  
  364.   if (from_tty)
  365.     printf ("Continuing at 0x%x.\n", addr);
  366.  
  367.   proceed (addr, 0, 0);
  368. }
  369.  
  370. #ifndef KGDB
  371. /* Continue program giving it specified signal.  */
  372.  
  373. static void
  374. signal_command (signum_exp, from_tty)
  375.      char *signum_exp;
  376.      int from_tty;
  377. {
  378.   register int signum;
  379.  
  380.   dont_repeat ();        /* Too dangerous.  */
  381.   ERROR_NO_INFERIOR;
  382.  
  383.   if (!signum_exp)
  384.     error_no_arg ("signal number");
  385.  
  386.   signum = parse_and_eval_address (signum_exp);
  387.  
  388.   clear_proceed_status ();
  389.  
  390.   if (from_tty)
  391.     printf ("Continuing with signal %d.\n", signum);
  392.  
  393.   proceed (stop_pc, signum, 0);
  394. }
  395.  
  396. /* Execute a "stack dummy", a piece of code stored in the stack
  397.    by the debugger to be executed in the inferior.
  398.  
  399.    To call: first, do PUSH_DUMMY_FRAME.
  400.    Then push the contents of the dummy.  It should end with a breakpoint insn.
  401.    Then call here, passing address at which to start the dummy.
  402.  
  403.    The contents of all registers are saved before the dummy frame is popped
  404.    and copied into the buffer BUFFER.
  405.  
  406.    The dummy's frame is automatically popped whenever that break is hit.
  407.    If that is the first time the program stops, run_stack_dummy
  408.    returns to its caller with that frame already gone.
  409.    Otherwise, the caller never gets returned to.  */
  410.  
  411. /* 4 => return instead of letting the stack dummy run.  */
  412.  
  413. static int stack_dummy_testing = 0;
  414.  
  415. void
  416. run_stack_dummy (addr, buffer)
  417.      CORE_ADDR addr;
  418.      REGISTER_TYPE *buffer;
  419. {
  420.   /* Now proceed, having reached the desired place.  */
  421.   clear_proceed_status ();
  422.   if (stack_dummy_testing & 4)
  423.     {
  424.       POP_FRAME;
  425.       return;
  426.     }
  427.   proceed (addr, 0, 0);
  428.  
  429.   if (!stop_stack_dummy)
  430.     error ("Cannot continue previously requested operation.");
  431.  
  432.   /* On return, the stack dummy has been popped already.  */
  433.  
  434.   bcopy (stop_registers, buffer, sizeof stop_registers);
  435. }
  436. #endif
  437.  
  438. /* Proceed until we reach the given line as argument or exit the
  439.    function.  When called with no argument, proceed until we reach a
  440.    different source line with pc greater than our current one or exit
  441.    the function.  We skip calls in both cases.
  442.  
  443.    The effect of this command with an argument is identical to setting
  444.    a momentary breakpoint at the line specified and executing
  445.    "finish".
  446.  
  447.    Note that eventually this command should probably be changed so
  448.    that only source lines are printed out when we hit the breakpoint
  449.    we set.  I'm going to postpone this until after a hopeful rewrite
  450.    of wait_for_inferior and the proceed status code. -- randy */
  451.  
  452. void
  453. until_next_command (arg, from_tty)
  454.      char *arg;
  455.      int from_tty;
  456. {
  457.   FRAME frame;
  458.   CORE_ADDR pc;
  459.   struct symbol *func;
  460.   struct symtab_and_line sal;
  461.     
  462.   clear_proceed_status ();
  463.  
  464.   frame = get_current_frame ();
  465.  
  466.   /* Step until either exited from this function or greater
  467.      than the current line (if in symbolic section) or pc (if
  468.      not). */
  469.  
  470.   pc = read_pc ();
  471.   func = find_pc_function (pc);
  472.   
  473.   if (!func)
  474.     {
  475.       int misc_func = find_pc_misc_function (pc);
  476.       
  477.       if (misc_func != -1)
  478.     error ("Execution is not within a known function.");
  479.       
  480.       step_range_start = misc_function_vector[misc_func].address;
  481.       step_range_end = pc;
  482.     }
  483.   else
  484.     {
  485.       sal = find_pc_line (pc, 0);
  486.       
  487.       step_range_start = BLOCK_START (SYMBOL_BLOCK_VALUE (func));
  488.       step_range_end = sal.end;
  489.     }
  490.   
  491.   step_over_calls = 1;
  492.   step_frame_address = FRAME_FP (frame);
  493.   
  494.   step_multi = 0;        /* Only one call to proceed */
  495.   
  496.   proceed (-1, -1, 1);
  497. }
  498.  
  499. void 
  500. until_command (arg, from_tty)
  501.      char *arg;
  502.      int from_tty;
  503. {
  504.   if (!have_inferior_p ())
  505.     error ("The program is not being run.");
  506.  
  507.   if (arg)
  508.     until_break_command (arg, from_tty);
  509.   else
  510.     until_next_command (arg, from_tty);
  511. }
  512.  
  513. /* "finish": Set a temporary breakpoint at the place
  514.    the selected frame will return to, then continue.  */
  515.  
  516. static void
  517. finish_command (arg, from_tty)
  518.      char *arg;
  519.      int from_tty;
  520. {
  521.   struct symtab_and_line sal;
  522.   register FRAME frame;
  523.   struct frame_info *fi;
  524.   register struct symbol *function;
  525.  
  526.   if (!have_inferior_p ())
  527.     error ("The program is not being run.");
  528.   if (arg)
  529.     error ("The \"finish\" command does not take any arguments.");
  530.  
  531.   frame = get_prev_frame (selected_frame);
  532.   if (frame == 0)
  533.     error ("\"finish\" not meaningful in the outermost frame.");
  534.  
  535.   clear_proceed_status ();
  536.  
  537.   fi = get_frame_info (frame);
  538.   sal = find_pc_line (fi->pc, 0);
  539.   sal.pc = fi->pc;
  540.   set_momentary_breakpoint (sal, frame);
  541.  
  542.   /* Find the function we will return from.  */
  543.  
  544.   fi = get_frame_info (selected_frame);
  545.   function = find_pc_function (fi->pc);
  546.  
  547.   if (from_tty)
  548.     {
  549.       printf ("Run till exit from ");
  550.       print_selected_frame ();
  551.     }
  552.  
  553.   proceed (-1, -1, 0);
  554.  
  555.   if (stop_breakpoint == -3 && function != 0)
  556.     {
  557.       struct type *value_type;
  558.       register value val;
  559.       CORE_ADDR funcaddr;
  560.  
  561.       value_type = TYPE_TARGET_TYPE (SYMBOL_TYPE (function));
  562.       if (!value_type)
  563.     fatal ("internal: finish_command: function has no target type");
  564.       
  565.       if (TYPE_CODE (value_type) == TYPE_CODE_VOID)
  566.     return;
  567.  
  568.       funcaddr = BLOCK_START (SYMBOL_BLOCK_VALUE (function));
  569.  
  570.       val = value_being_returned (value_type, stop_registers,
  571.                   using_struct_return (function,
  572.                                funcaddr,
  573.                                value_type));
  574.  
  575.       printf ("Value returned is $%d = ", record_latest_value (val));
  576.       value_print (val, stdout, 0, Val_no_prettyprint);
  577.       putchar ('\n');
  578.     }
  579. }
  580. #ifdef KGDB
  581. static void
  582. machine_info ()
  583. {
  584.   extern char *hostName;
  585.   if (inferior_pid == 0 || hostName == (char *) 0)
  586.     {
  587.       printf ("A machine is not current attached.\n");
  588.       return;
  589.     }
  590.  
  591.   printf ("Machine %s is  stopped at 0x%x.\n",
  592.        hostName,stop_pc);
  593.   if (stop_step)
  594.     printf ("It stopped after being stepped.\n");
  595.   else if (stop_breakpoint)
  596.     printf ("It stopped at breakpoint %d.\n", stop_breakpoint);
  597.   else if (stop_signal)
  598.     printf ("It stopped with signal %d (%s).\n",
  599.         stop_signal, sys_siglist[stop_signal]);
  600.  
  601.   printf ("\nType \"info stack\" or \"info reg\" for more information.\n");
  602. }
  603. tty_command() { }
  604. run_stack_dummy() { }
  605. #else
  606.  
  607. static void
  608. program_info ()
  609. {
  610.   if (inferior_pid == 0)
  611.     {
  612.       printf ("The program being debugged is not being run.\n");
  613.       return;
  614.     }
  615.  
  616.   printf ("Program being debugged is in process %d, stopped at 0x%x.\n",
  617.       inferior_pid, stop_pc);
  618.   if (stop_step)
  619.     printf ("It stopped after being stepped.\n");
  620.   else if (stop_breakpoint > 0)
  621.     printf ("It stopped at breakpoint %d.\n", stop_breakpoint);
  622.   else if (stop_signal)
  623.     printf ("It stopped with signal %d (%s).\n",
  624.         stop_signal, sys_siglist[stop_signal]);
  625.  
  626.   printf ("\nType \"info stack\" or \"info reg\" for more information.\n");
  627. }
  628.  
  629. static void
  630. environment_info (var)
  631.      char *var;
  632. {
  633.   if (var)
  634.     {
  635.       register char *val = get_in_environ (inferior_environ, var);
  636.       if (val)
  637.     printf ("%s = %s\n", var, val);
  638.       else
  639.     printf ("Environment variable \"%s\" not defined.\n", var);
  640.     }
  641.   else
  642.     {
  643.       register char **vector = environ_vector (inferior_environ);
  644.       while (*vector)
  645.     printf ("%s\n", *vector++);
  646.     }
  647. }
  648.  
  649. static void
  650. set_environment_command (arg)
  651.      char *arg;
  652. {
  653.   register char *p, *val, *var;
  654.   int nullset = 0;
  655.  
  656.   if (arg == 0)
  657.     error_no_arg ("environment variable and value");
  658.  
  659.   /* Find seperation between variable name and value */
  660.   p = (char *) index (arg, '=');
  661.   val = (char *) index (arg, ' ');
  662.  
  663.   if (p != 0 && val != 0)
  664.     {
  665.       /* We have both a space and an equals.  If the space is before the
  666.      equals and the only thing between the two is more space, use
  667.      the equals */
  668.       if (p > val)
  669.     while (*val == ' ')
  670.       val++;
  671.  
  672.       /* Take the smaller of the two.  If there was space before the
  673.      "=", they will be the same right now. */
  674.       p = arg + min (p - arg, val - arg);
  675.     }
  676.   else if (val != 0 && p == 0)
  677.     p = val;
  678.  
  679.   if (p == arg)
  680.     error_no_arg ("environment variable to set");
  681.  
  682.   if (p == 0 || p[1] == 0)
  683.     {
  684.       nullset = 1;
  685.       if (p == 0)
  686.     p = arg + strlen (arg);    /* So that savestring below will work */
  687.     }
  688.   else
  689.     {
  690.       /* Not setting variable value to null */
  691.       val = p + 1;
  692.       while (*val == ' ' || *val == '\t')
  693.     val++;
  694.     }
  695.  
  696.   while (p != arg && (p[-1] == ' ' || p[-1] == '\t')) p--;
  697.  
  698.   var = savestring (arg, p - arg);
  699.   if (nullset)
  700.     {
  701.       printf ("Setting environment variable \"%s\" to null value.\n", var);
  702.       set_in_environ (inferior_environ, var, "");
  703.     }
  704.   else
  705.     set_in_environ (inferior_environ, var, val);
  706.   free (var);
  707. }
  708.  
  709. static void
  710. unset_environment_command (var)
  711.      char *var;
  712. {
  713.   if (var == 0)
  714.     error_no_arg ("environment variable");
  715.  
  716.   unset_in_environ (inferior_environ, var);
  717. }
  718. #endif
  719.  
  720. /* Read an integer from debugged memory, given address and number of bytes.  */
  721.  
  722. long
  723. read_memory_integer (memaddr, len)
  724.      CORE_ADDR memaddr;
  725.      int len;
  726. {
  727.   char cbuf;
  728.   short sbuf;
  729.   int ibuf;
  730.   long lbuf;
  731.   int result_err;
  732.   extern int sys_nerr;
  733.   extern char *sys_errlist[];
  734.  
  735.   if (len == sizeof (char))
  736.     {
  737.       result_err = read_memory (memaddr, &cbuf, len);
  738.       if (result_err)
  739.     error ("Error reading memory address 0x%x: %s (%d).",
  740.            memaddr, (result_err < sys_nerr ?
  741.              sys_errlist[result_err] :
  742.              "uknown error"), result_err);
  743.       return cbuf;
  744.     }
  745.   if (len == sizeof (short))
  746.     {
  747.       result_err = read_memory (memaddr, &sbuf, len);
  748.       if (result_err)
  749.     error ("Error reading memory address 0x%x: %s (%d).",
  750.            memaddr, (result_err < sys_nerr ?
  751.              sys_errlist[result_err] :
  752.              "uknown error"), result_err);
  753.       return sbuf;
  754.     }
  755.   if (len == sizeof (int))
  756.     {
  757.       result_err = read_memory (memaddr, &ibuf, len);
  758.       if (result_err)
  759.     error ("Error reading memory address 0x%x: %s (%d).",
  760.            memaddr, (result_err < sys_nerr ?
  761.              sys_errlist[result_err] :
  762.              "uknown error"), result_err);
  763.       return ibuf;
  764.     }
  765.   if (len == sizeof (lbuf))
  766.     {
  767.       result_err = read_memory (memaddr, &lbuf, len);
  768.       if (result_err)
  769.     error ("Error reading memory address 0x%x: %s (%d).",
  770.            memaddr, (result_err < sys_nerr ?
  771.              sys_errlist[result_err] :
  772.              "uknown error"), result_err);
  773.       return lbuf;
  774.     }
  775.   error ("Cannot handle integers of %d bytes.", len);
  776. }
  777.  
  778. CORE_ADDR
  779. read_pc ()
  780. {
  781.   return (CORE_ADDR) read_register (PC_REGNUM);
  782. }
  783.  
  784. void
  785. write_pc (val)
  786.      CORE_ADDR val;
  787. {
  788.   write_register (PC_REGNUM, (long) val);
  789. #ifdef NPC_REGNUM
  790.   write_register (NPC_REGNUM, (long) val+4);
  791. #endif
  792. }
  793.  
  794. char *reg_names[] = REGISTER_NAMES;
  795.  
  796. static void
  797. registers_info (addr_exp)
  798.      char *addr_exp;
  799. {
  800.   register int i;
  801.   int regnum;
  802.  
  803.   if (!have_inferior_p () && !have_core_file_p ())
  804.     error ("No inferior or core file");
  805.  
  806.   if (addr_exp)
  807.     {
  808.       if (*addr_exp >= '0' && *addr_exp <= '9')
  809.     regnum = atoi (addr_exp);
  810.       else
  811.     {
  812.       register char *p = addr_exp;
  813.       if (p[0] == '$')
  814.         p++;
  815.       for (regnum = 0; regnum < NUM_REGS; regnum++)
  816.         if (!strcmp (p, reg_names[regnum]))
  817.           break;
  818.       if (regnum == NUM_REGS)
  819.         error ("%s: invalid register name.", addr_exp);
  820.     }
  821.     }
  822.   else
  823.     printf_filtered ("Reg\tContents\n\n");
  824.  
  825.   for (i = 0; i < NUM_REGS; i++)
  826.     {
  827.       unsigned char raw_buffer[MAX_REGISTER_RAW_SIZE];
  828.       unsigned char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE];
  829.       REGISTER_TYPE val;
  830.  
  831.       if (addr_exp != 0 && i != regnum)
  832.     continue;
  833.  
  834.       /* Get the data in raw format, then convert also to virtual format.  */
  835.       read_relative_register_raw_bytes (i, raw_buffer);
  836.       REGISTER_CONVERT_TO_VIRTUAL (i, raw_buffer, virtual_buffer);
  837.  
  838.       printf_filtered ("%s\t", reg_names[i]);
  839.  
  840.       /* If virtual format is floating, print it that way.  */
  841.       if (TYPE_CODE (REGISTER_VIRTUAL_TYPE (i)) == TYPE_CODE_FLT
  842.       && ! INVALID_FLOAT (virtual_buffer, REGISTER_VIRTUAL_SIZE (i)))
  843.     val_print (REGISTER_VIRTUAL_TYPE (i), virtual_buffer, 0,
  844.            stdout, 0, 1, 0, Val_pretty_default);
  845.       /* Else if virtual format is too long for printf,
  846.      print in hex a byte at a time.  */
  847.       else if (REGISTER_VIRTUAL_SIZE (i) > sizeof (long))
  848.     {
  849.       register int j;
  850.       printf_filtered ("0x");
  851.       for (j = 0; j < REGISTER_VIRTUAL_SIZE (i); j++)
  852.         printf_filtered ("%02x", virtual_buffer[j]);
  853.     }
  854.       /* Else print as integer in hex and in decimal.  */
  855.       else
  856.     {
  857.       long val;
  858.  
  859.       bcopy (virtual_buffer, &val, sizeof (long));
  860.       if (val == 0)
  861.         printf_filtered ("0");
  862.       else
  863.         printf_filtered ("0x%08x  %d", val, val);
  864.     }
  865.  
  866.       /* If register has different raw and virtual formats,
  867.      print the raw format in hex now.  */
  868.  
  869.       if (REGISTER_CONVERTIBLE (i))
  870.     {
  871.       register int j;
  872.  
  873.       printf_filtered ("  (raw 0x");
  874.       for (j = 0; j < REGISTER_RAW_SIZE (i); j++)
  875.         printf_filtered ("%02x", raw_buffer[j]);
  876.       printf_filtered (")");
  877.     }
  878.       printf_filtered ("\n");
  879.     }
  880.  
  881.   printf_filtered ("Contents are relative to selected stack frame.\n");
  882. }
  883. #ifndef KGDB
  884.  
  885. #ifdef ATTACH_DETACH
  886. #define PROCESS_ATTACH_ALLOWED 1
  887. #else
  888. #define PROCESS_ATTACH_ALLOWED 0
  889. #endif
  890. /*
  891.  * TODO:
  892.  * Should save/restore the tty state since it might be that the
  893.  * program to be debugged was started on this tty and it wants
  894.  * the tty in some state other than what we want.  If it's running
  895.  * on another terminal or without a terminal, then saving and
  896.  * restoring the tty state is a harmless no-op.
  897.  * This only needs to be done if we are attaching to a process.
  898.  */
  899.  
  900. /*
  901.  * attach_command --
  902.  * takes a program started up outside of gdb and ``attaches'' to it.
  903.  * This stops it cold in its tracks and allows us to start tracing it.
  904.  * For this to work, we must be able to send the process a
  905.  * signal and we must have the same effective uid as the program.
  906.  */
  907. static void
  908. attach_command (args, from_tty)
  909.      char *args;
  910.      int from_tty;
  911. {
  912.   char *exec_file;
  913.   int pid;
  914.   int remote = 0;
  915.  
  916.   dont_repeat();
  917.  
  918.   if (!args)
  919.     error_no_arg ("process-id or device file to attach");
  920.  
  921.   while (*args == ' ' || *args == '\t') args++;
  922.  
  923.   if (args[0] == '/')
  924.     remote = 1;
  925.   else
  926. #ifndef ATTACH_DETACH
  927.     error ("Can't attach to a process on this machine.");
  928. #else
  929. #ifdef sprite
  930.     /*
  931.      * Since Sprite pids are displayed in hex the atoi doesn't work. We
  932.      * allow the attach command to use any C expression.
  933.      */
  934.     pid = value_as_long (evaluate_expression (parse_c_expression (args)));
  935. #else
  936.     pid = atoi (args);
  937. #endif
  938. #endif
  939.  
  940.   if (inferior_pid)
  941.     {
  942.       if (query ("A program is being debugged already.  Kill it? "))
  943.     kill_inferior ();
  944.       else
  945.     error ("Inferior not killed.");
  946.     }
  947.  
  948.   exec_file = (char *) get_exec_file (1);
  949.  
  950.   if (from_tty)
  951.     {
  952.       if (remote)
  953.     printf ("Attaching remote machine\n");
  954.       else
  955.     printf ("Attaching program: %s pid %d\n",
  956.         exec_file, pid);
  957.       fflush (stdout);
  958.     }
  959.  
  960. #ifdef ATTACH_DETACH
  961.   if (remote)
  962.     {
  963. #endif
  964.       remote_open (args, from_tty);
  965.       start_remote ();
  966. #ifdef ATTACH_DETACH
  967.     }
  968.   else
  969.     attach_program (pid);
  970. #endif
  971. }
  972.  
  973. /*
  974.  * detach_command --
  975.  * takes a program previously attached to and detaches it.
  976.  * The program resumes execution and will no longer stop
  977.  * on signals, etc.  We better not have left any breakpoints
  978.  * in the program or it'll die when it hits one.  For this
  979.  * to work, it may be necessary for the process to have been
  980.  * previously attached.  It *might* work if the program was
  981.  * started via the normal ptrace (PTRACE_TRACEME).
  982.  */
  983.  
  984. static void
  985. detach_command (args, from_tty)
  986.      char *args;
  987.      int from_tty;
  988. {
  989.   int signal = 0;
  990.  
  991. #ifdef ATTACH_DETACH
  992.   if (inferior_pid)
  993.     {
  994.       if (from_tty)
  995.     {
  996.       char *exec_file = (char *)get_exec_file (0);
  997.       if (exec_file == 0)
  998.         exec_file = "";
  999.       printf ("Detaching program: %s pid %d\n",
  1000.           exec_file, inferior_pid);
  1001.       fflush (stdout);
  1002.     }
  1003.       if (args)
  1004.     signal = atoi (args);
  1005.       
  1006.       detach (signal);
  1007.       inferior_pid = 0;
  1008.     }
  1009.   else
  1010. #endif
  1011.     {
  1012.       if (!remote_debugging)
  1013.     error ("Not currently attached to subsidiary or remote process.");
  1014.  
  1015.       if (args)
  1016.     error ("Argument given to \"detach\" when remotely debugging.");
  1017.       
  1018.       remote_close (from_tty);
  1019.     }
  1020. }
  1021. #endif
  1022. /* ARGUSUED */
  1023. static void
  1024. float_info (addr_exp)
  1025.      char *addr_exp;
  1026. {
  1027. #ifdef FLOAT_INFO
  1028.     FLOAT_INFO;
  1029. #else
  1030.     printf ("No floating point info available for this processor.\n");
  1031. #endif
  1032. }
  1033.  
  1034. extern struct cmd_list_element *setlist, *deletelist;
  1035.  
  1036. void
  1037. _initialize_infcmd ()
  1038. {
  1039. #ifndef KGDB
  1040.   add_com ("tty", class_run, tty_command,
  1041.        "Set terminal for future runs of program being debugged.");
  1042.  
  1043.   add_cmd ("args", class_run, set_args_command,
  1044.        "Specify arguments to give program being debugged when it is started.\n\
  1045. Follow this command with any number of args, to be passed to the program.",
  1046.        &setlist);
  1047.  
  1048.   add_info ("environment", environment_info,
  1049.         "The environment to give the program, or one variable's value.\n\
  1050. With an argument VAR, prints the value of environment variable VAR to\n\
  1051. give the program being debugged.  With no arguments, prints the entire\n\
  1052. environment to be given to the program.");
  1053.  
  1054.   add_cmd ("environment", class_run, unset_environment_command,
  1055.        "Cancel environment variable VAR for the program.\n\
  1056. This does not affect the program until the next \"run\" command.",
  1057.        &deletelist);
  1058.  
  1059.   add_cmd ("environment", class_run, set_environment_command,
  1060.        "Set environment variable value to give the program.\n\
  1061. Arguments are VAR VALUE where VAR is variable name and VALUE is value.\n\
  1062. VALUES of environment variables are uninterpreted strings.\n\
  1063. This does not affect the program until the next \"run\" command.",
  1064.        &setlist);
  1065.  
  1066. #ifdef ATTACH_DETACH
  1067.  add_com ("attach", class_run, attach_command,
  1068.         "Attach to a process that was started up outside of GDB.\n\
  1069. This command may take as argument a process id or a device file.\n\
  1070. For a process id, you must have permission to send the process a signal,\n\
  1071. and it must have the same effective uid as the debugger.\n\
  1072. For a device file, the file must be a connection to a remote debug server.\n\n\
  1073. Before using \"attach\", you must use the \"exec-file\" command\n\
  1074. to specify the program running in the process,\n\
  1075. and the \"symbol-file\" command to load its symbol table.");
  1076. #else
  1077.  add_com ("attach", class_run, attach_command,
  1078.         "Attach to a process that was started up outside of GDB.\n\
  1079. This commands takes as an argument the name of a device file.\n\
  1080. This file must be a connection to a remote debug server.\n\n\
  1081. Before using \"attach\", you must use the \"exec-file\" command\n\
  1082. to specify the program running in the process,\n\
  1083. and the \"symbol-file\" command to load its symbol table.");
  1084. #endif
  1085.   add_com ("detach", class_run, detach_command,
  1086.        "Detach the process previously attached.\n\
  1087. The process is no longer traced and continues its execution.");
  1088.  
  1089.   add_com ("signal", class_run, signal_command,
  1090.        "Continue program giving it signal number SIGNUMBER.");
  1091. #endif
  1092.   add_com ("stepi", class_run, stepi_command,
  1093.        "Step one instruction exactly.\n\
  1094. Argument N means do this N times (or till program stops for another reason).");
  1095.   add_com_alias ("si", "stepi", class_alias, 0);
  1096.  
  1097.   add_com ("nexti", class_run, nexti_command,
  1098.        "Step one instruction, but proceed through subroutine calls.\n\
  1099. Argument N means do this N times (or till program stops for another reason).");
  1100.   add_com_alias ("ni", "nexti", class_alias, 0);
  1101.  
  1102.   add_com ("finish", class_run, finish_command,
  1103.        "Execute until selected stack frame returns.\n\
  1104. Upon return, the value returned is printed and put in the value history.");
  1105.  
  1106.   add_com ("next", class_run, next_command,
  1107.        "Step program, proceeding through subroutine calls.\n\
  1108. Like the \"step\" command as long as subroutine calls do not happen;\n\
  1109. when they do, the call is treated as one instruction.\n\
  1110. Argument N means do this N times (or till program stops for another reason).");
  1111.   add_com_alias ("n", "next", class_run, 1);
  1112.  
  1113.   add_com ("step", class_run, step_command,
  1114.        "Step program until it reaches a different source line.\n\
  1115. Argument N means do this N times (or till program stops for another reason).");
  1116.   add_com_alias ("s", "step", class_run, 1);
  1117.  
  1118.   add_com ("until", class_run, until_command,
  1119.        "Execute until the program reaches a source line greater than the current\n\
  1120. or a specified line or address or function (same args as break command).\n\
  1121. Execution will also stop upon exit from the current stack frame.");
  1122.   add_com_alias ("u", "until", class_run, 1);
  1123.   
  1124.   add_com ("jump", class_run, jump_command,
  1125.        "Continue program being debugged at specified line or address.\n\
  1126. Give as argument either LINENUM or *ADDR, where ADDR is an expression\n\
  1127. for an address to start at.");
  1128.  
  1129.   add_com ("cont", class_run, cont_command,
  1130.        "Continue program being debugged, after signal or breakpoint.\n\
  1131. If proceeding from breakpoint, a number N may be used as an argument:\n\
  1132. then the same breakpoint won't break until the Nth time it is reached.");
  1133.   add_com_alias ("c", "cont", class_run, 1);
  1134. #ifndef KGDB
  1135.   add_com ("run", class_run, run_command,
  1136.        "Start debugged program.  You may specify arguments to give it.\n\
  1137. Args may include \"*\", or \"[...]\"; they are expanded using \"sh\".\n\
  1138. Input and output redirection with \">\", \"<\", or \">>\" are also allowed.\n\n\
  1139. With no arguments, uses arguments last specified (with \"run\" or \"set args\".\n\
  1140. To cancel previous arguments and run with no arguments,\n\
  1141. use \"set args\" without arguments.");
  1142.   add_com_alias ("r", "run", class_run, 1);
  1143. #endif
  1144.   add_info ("registers", registers_info,
  1145.         "List of registers and their contents, for selected stack frame.\n\
  1146. Register name as argument means describe only that register.");
  1147.  
  1148. #ifdef KGDB
  1149.  
  1150.   add_info ("machine", machine_info,
  1151.         "Execution status of the machine being debugged.");
  1152. #else
  1153.   add_info ("program", program_info,
  1154.         "Execution status of the program.");
  1155.  
  1156.   add_info ("float", float_info,
  1157.         "Print the status of the floating point unit\n");
  1158.   inferior_args = savestring (" ", 1); /* By default, no args.  */
  1159.   inferior_environ = make_environ ();
  1160.   init_environ (inferior_environ);
  1161. #endif
  1162. }
  1163.  
  1164.